<template>
	<view class="container">
		<view class="form">
			<text class="title">注册</text>
			<view class="input-group"><input v-model="username" placeholder="请输入用户名" /></view>
			<view class="input-group"><input v-model="password" placeholder=" 请输入密码" /></view>
			<view class="input-group"><input v-model="confirmPassword" placeholder="请确认密码" /></view>
			<button class="btn" type="primary" @click="handleLogin">注册</button>
			<view class="link-t"><text>已有账号？</text></view>
			<view class="link">
				<navigator url="/pages/login/login" open-type="reLaunch">去登录
				</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: ''
			}
		},
		methods: {
			handleLogin() {
				if (!this.username || !this.password || !this.confirmPassword) {
					uni.showToast({
						title: '请输入完整信息',
						icon: 'none'
					})
					return
				}
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '注册中...'
				})
				uniCloud.callFunction({
						name: 'addUser',
						data: {
							'name': this.username,
							'password': this.password
						},
						success: (msg) => {
							console.log("addUser CallBack" + msg);
							setTimeout(() => {
								uni.hideLoading()
								uni.showToast({
									title: '注册成功'
								})
							}, 1000)
							uni.navigateBack();
						},fail: (res) => {
							console.warn("addUser CallBack fail" + res);
						}
					})
				}
			}
		}
</script>

<style>

</style>